﻿using AhCha.Fortunate.Api.Hubs;
using Microsoft.AspNetCore.Mvc;
using AhCha.Fortunate.ModelsDto;
using Microsoft.AspNetCore.SignalR;
using AhCha.Fortunate.Common.Const;
using AhCha.Fortunate.IService.MySQL;
using AhCha.Fortunate.ModelsDto.MySQL.ApiRequestLogDto;
using AhCha.Fortunate.ModelsDto.MySQL.ApiExceptionLogDto;



namespace AhCha.Fortunate.Api.Controllers
{
    /// <summary>
    /// 日志api
    /// </summary>
    [ApiExplorerSettings(GroupName = SwaggerGroupName.SystemModules)]
    public class LogController : BaseApiController
    {
        private readonly IApiExceptionLogService _IApiExceptionLogService;
        private readonly IApiRequestLogService _IApiRequestLogService;
        private readonly IHubContext<FortunateHub> _hubContext;

        public LogController(IApiExceptionLogService iApiExceptionLogService, IApiRequestLogService iApiRequestLogService, IHubContext<FortunateHub> hubContext)
        {
            _IApiExceptionLogService = iApiExceptionLogService;
            _IApiRequestLogService = iApiRequestLogService;
            _hubContext = hubContext;
        }

        /// <summary>
        /// 获取接口异常记录日志
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<SqlSugarPagedList<ApiExceptionLogOutput>> GetExceptionPage([FromQuery] QueryApiExceptionLogInput input)
        {
            return await _IApiExceptionLogService.GetApiExceptionLogPage(input);
        }

        /// <summary>
        /// 获取接口请求记录日志
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<SqlSugarPagedList<ApiRequestLogOutput>> GetRequestLogPage([FromQuery] QueryApiRequestLogInput input)
        {
            return await _IApiRequestLogService.GetApiRequestLogPage(input);
        }

        /// <summary>
        /// 在Controller中发送信息SignalR（通讯）
        /// </summary>
        /// <param name="input">接口数据</param>
        /// <returns></returns>
        [HttpGet]
        public async Task SendMessage(string input)
        {
            await _hubContext.Clients.All.SendAsync(SendMethod.ReceiveMessage, input);
        }
    }
}
